﻿<!DOCTYPE html>
<html>
<head>
    <title>JS Strings Exercise 2</title>
    <link href="styles/js-console.css" rel="stylesheet" />
</head>
<body>
    <h1>Function to check if in a given expression the brackets are put correctly.</h1>
    <label for="str-input">String:</label>
    <input type="text" id="str-input" />
    <button onclick="execute()">execute</button>
    <div id="js-console"></div>
    <script src="scripts/js-console.js"></script>
    <script>
        function checkBrackets(str)
        {
            var openBrackets = 0;
            var retStr = "";
            var strLen = str.length;
            for (var idx = 0; idx < strLen; idx++)
            {
                if (str.charAt(idx) === "(")
                {
                    openBrackets++;
                }
                else if (str.charAt(idx) === ")")
                {
                    openBrackets--;
                    if (openBrackets < 0)
                    {
                        return false;
                    }
                }
            }

            if (openBrackets > 0)
            {
                return false;
            }

            return true;
        }

        function execute()
        {
            var inpStr = jsConsole.read("#str-input");

            jsConsole.writeLine("Brackets status: " + checkBrackets(inpStr));
        }
    </script>
</body>
</html>
